// pages/my/days/days.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    datetypes: ['公历', '阴历']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadMemory();
    var endtime = app.currentDate();
    console.log(endtime);
    this.setData({
      endtime: endtime
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadMemory: function () {
    var url = '/silo/h5/bigday/list/2511.json';
    var params = {
      auth: app.globalData.authToken
    };
    app.loadDataFromServer(url, this.loadMemoryCallBack, JSON.stringify(params), 'post');
  },
  loadMemoryCallBack: function (res) {
    console.log(res);
    if (res.protocError == 0) {
      var items = res.items;
      for (var i = 0; i < items.length; i++) {
        items[i].dateval = items[i].year + '-' + items[i].month + '-' + items[i].day;
        if (!items[i].title){
          items[i].title = '';
        }
      }
      this.setData({
        memorylist: items
      });
    }
  },
  bindDateChange: function (e) {
    var dateval = e.detail.value;
    var mindex = e.currentTarget.dataset.index;
    var vals = dateval.split('-');
    var memory = this.data.memorylist;
    memory[mindex].year = parseInt(vals[0]);
    memory[mindex].month = parseInt(vals[1]);
    memory[mindex].day = parseInt(vals[2]);
    this.setData({
      memorylist: memory
    });
  },
  dateTypeChange: function (e) {
    console.log(e.detail.value);
    var memory = this.data.memorylist;
    var mindex = e.currentTarget.dataset.index;
    memory[mindex].lunar = e.detail.value == 0 ? false : true;
    this.setData({
      memorylist: memory
    });
  },
  nameInput: function (e) {
    var mindex = e.currentTarget.dataset.index;
    var memory = this.data.memorylist;
    memory[mindex].title = e.detail.value;
    this.setData({
      memorylist: memory
    });
  },
  saveMemoryAction:function(){
    wx.showLoading({
      title: '',
    });
    var memory = this.data.memorylist;
    for(var i=0;i<memory.length;i++){
      if(!memory.title){
        memory.title='';
      }
    }
    var url = '/silo/h5/bigday/update/2513.json';
    var params = {
      auth: app.globalData.authToken,
      items: this.data.memorylist
    };
    app.loadDataFromServer(url, this.saveMemoryCallBack, JSON.stringify(params), 'post');
  },
  saveMemoryCallBack:function(res){
    console.log(res);
    wx.hideLoading();
    if (res.protocError == 0){
      wx.showToast({
        title: '保存成功',
      });
    }
  }
})